package com.xique.door.project.miniprogram.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.door.bean.domain.AccessDevice;
import com.xique.door.bean.domain.BaseUser;
import com.xique.door.bean.domain.User;
import com.xique.door.bean.domain.UserDevice;
import com.xique.door.bean.search.UserDeviceSearch;
import com.xique.door.command.IDeviceUserCommandService;
import com.xique.door.mapper.BaseUserMapper;
import com.xique.door.mapper.UserMapper;
import com.xique.door.project.miniprogram.bean.request.MiniBaseUserRequest;
import com.xique.door.project.miniprogram.bean.vo.MiniBaseUserVO;
import com.xique.door.project.miniprogram.service.IMiniBaseUserService;
import com.xique.door.service.IAccessDeviceService;
import com.xique.door.service.IUserDeviceService;
import com.xique.door.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class MiniBaseUserServiceImpl implements IMiniBaseUserService {

    @Autowired
    private BaseUserMapper baseUserMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IDeviceUserCommandService deviceUserCommandService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserDeviceService userDeviceService;

    @Autowired
    private IAccessDeviceService accessDeviceService;


    @Override
    public MiniBaseUserVO selectBaseUserById() {
        BaseUser baseUser = baseUserMapper.selectBaseUserById(SecurityUtils.getBaseUserId());
        MiniBaseUserVO miniBaseUserVO = new MiniBaseUserVO();
        miniBaseUserVO.setUserName(baseUser.getUserName());
        miniBaseUserVO.setGender(baseUser.getGender());
        miniBaseUserVO.setCellphone(baseUser.getCellphone());
        miniBaseUserVO.setFaceKey(baseUser.getFaceKey());
        miniBaseUserVO.setFaceUrl(baseUser.getFaceUrl());
        return miniBaseUserVO;
    }

    @Override
    public String updateBaseUser(MiniBaseUserRequest miniBaseUserRequest) {
        BaseUser baseUser = new BaseUser();
        baseUser.setId(SecurityUtils.getBaseUserId());
        baseUser.setUserName(miniBaseUserRequest.getUserName());
        baseUser.setGender(miniBaseUserRequest.getGender());
        baseUser.setFaceUrl(miniBaseUserRequest.getFaceUrl());
        baseUser.setFaceKey(miniBaseUserRequest.getFaceKey());
        baseUserMapper.updateBaseUser(baseUser);
        User userSearch = new User();
        userSearch.setBaseUserId(SecurityUtils.getBaseUserId());
        List<User> userList = userMapper.selectUsers(userSearch);
        getbaseUser(userList, miniBaseUserRequest);
        if (CollectionUtil.isNotEmpty(userList)) {
            List<Long> userDeviceIds = new ArrayList<>();
            userList.forEach(u -> {
                UserDeviceSearch search = new UserDeviceSearch();
                search.setUserId(u.getId());
                List<UserDevice> userDeviceList = userDeviceService.selectUserDeviceListBySearch(search);
                if (CollectionUtil.isNotEmpty(userDeviceList)) {
                    userDeviceList.forEach(e -> {
                        userDeviceIds.add(e.getDeviceId());
                    });
                }
            });
            List<AccessDevice> accessDeviceList = accessDeviceService.selectAccessDeviceByIds(userDeviceIds);
            if (CollectionUtil.isNotEmpty(accessDeviceList)) {
                userList.forEach(u -> {
                deviceUserCommandService.downDevicesUsers(accessDeviceList, Collections.singletonList(u));
                });
            }
        }

        return "修改成功";
    }

    public void getbaseUser(List<User> list, MiniBaseUserRequest miniBaseUserRequest) {

        for (User user2 : list) {
            if (StringUtils.isNotEmpty(miniBaseUserRequest.getUserName())) {
                user2.setUserName(miniBaseUserRequest.getUserName());
            }
            user2.setGender(miniBaseUserRequest.getGender());

            if (StringUtils.isNotEmpty(miniBaseUserRequest.getFaceUrl())) {
                user2.setFaceUrl(miniBaseUserRequest.getFaceUrl());
            }
            if (StringUtils.isNotEmpty(miniBaseUserRequest.getFaceKey())) {
                user2.setFaceKey(miniBaseUserRequest.getFaceKey());
            }
            user2.setUpdateUser(SecurityUtils.getBaseUserId());
            user2.setUserType(1);
            userService.updateUser(user2);
        }

    }

}
